package com.task.job.recharge;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.entity.Measuringpoint;
import com.entity.Recharge;
import com.file.service.MeasuringpointService;
import com.file.service.MeterService;
import com.file.service.TerminalService;
import com.interfaces.MeterDao;
import com.interfaces.RechargeDao;
import com.recharge.srevice.RechargeService;
import com.task.job.recharge.workflow.Workflow;
import com.task.job.recharge.workflow.WorkflowSTS645_Recharge;

@Component
public class RechargeJob implements Job {

	@Autowired
	private RechargeDao rechargeDao;
	@Autowired
	private MeterService meterService;
	@Autowired
	private TerminalService terminalService;
	@Autowired
	private MeasuringpointService measuringpointService;
	@Autowired
	private RechargeService rechargeService;
	@Autowired
	private MeterDao meterDao;
	@Value("${meter.keys}")
	private String keys;

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {

		// 查询具体的工单信息
		JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
		String id = jobDataMap.getString("taskid");
		Recharge recharge = rechargeDao.findOne(Integer.valueOf(id));

		// 查询协议
		String meteraddress = meterService.findMeteraddressByMeterid(recharge.getMeterid());
		String meterprotocol = meterService.findMeterprotocolByMeterid(recharge.getMeterid());
		Integer terminalid = measuringpointService.findTerminalidByMeterid(recharge.getMeterid());
		String terminalprotocol = terminalService.findTerminalprotocolByTerminalid(terminalid);
		String terminaladdress = terminalService.findTerminaladdressByTerminalid(terminalid);

		Measuringpoint measuringpoint = measuringpointService.findByTerminalidAndMeterid(terminalid,
				Integer.valueOf(recharge.getMeterid()));
		// 获取工作流
		Workflow workflow = null;
		switch (meterprotocol) {
		case "5":
			workflow = new WorkflowSTS645_Recharge(terminaladdress, terminalprotocol, meteraddress, meterprotocol,
					measuringpoint, rechargeService, meterDao, keys);
			break;

		default:
			break;
		}
		// 执行
		workflow.Doflow(recharge);

	}

}
